package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.dto.RealTimeMonitoring.InitMapStationDTO;
import com.woniu.dto.RealTimeMonitoring.StationChangeDTO;
import com.woniu.dto.RealTimeMonitoring.StationNameSearchResponseDTO;
import com.woniu.entity.Charger;
import com.woniu.entity.ChargingStation;
import com.woniu.mapper.ChargerMapper;
import com.woniu.service.ChargingStationService;
import com.woniu.mapper.ChargingStationMapper;
import com.woniu.util.ResponseData;
import com.woniu.util.ResponseEnum;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author Apricity
* @description 针对表【charging_station(充电站表)】的数据库操作Service实现
* @createDate 2025-02-13 20:16:58
*/
@Service
@AllArgsConstructor
public class ChargingStationServiceImpl extends ServiceImpl<ChargingStationMapper, ChargingStation>
    implements ChargingStationService{

    @Resource
    private ChargingStationMapper chargingStationMapper;

    @Resource
    private ChargerMapper chargerMapper;


    @Override
    public List<ChargingStation> getStationInfo(String stationName) {
        if (stationName != null&&!stationName.trim().equals("")) {
            return chargingStationMapper.selectList(new QueryWrapper<ChargingStation>().like("station_name", stationName));
        }
        return chargingStationMapper.selectList(null);
    }

    @Override
    public List<Charger> chargerListC(Map<String, Object> param) {
        String chargerName = (String) param.get("chargerName");
        ObjectMapper objectMapper = new ObjectMapper();
        List<ChargingStation> stationData = objectMapper.convertValue(
                param.get("stationData"),
                new TypeReference<List<ChargingStation>>() {}
        );

        List<Charger> list = new ArrayList<>();

        for (ChargingStation station : stationData) {
            Long id = station.getId();

            List<Charger> chargers = chargerMapper.selectList(new QueryWrapper<Charger>()
                    .eq("station_id", id)
                    .like("charger_name", chargerName));

            list.addAll(chargers);
        }

        return list;
    }

    @Override
    public ResponseData<?> stationNameSearch(String stationName) {
        ChargingStation chargingStation = chargingStationMapper.stationNameSearchTop1(stationName);
        if (chargingStation == null) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        StationNameSearchResponseDTO responseDTO =chargingStationMapper.selectChargersByStationId(chargingStation.getId());
        if (responseDTO == null) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        responseDTO.setId(chargingStation.getId());
        responseDTO.setStationName(chargingStation.getStationName());
        responseDTO.setStationLocation(chargingStation.getStationLocation());
        return new ResponseData<>().ok(responseDTO);
    }


    public ResponseData<?> stationChange(StationChangeDTO stationChangeDTO) {
        ChargingStation chargingStation=new ChargingStation();
        if (stationChangeDTO.getOrder().equals(0)) {
            chargingStation = chargingStationMapper.stationChangeLastById(stationChangeDTO.getId());
        } else {
            chargingStation = chargingStationMapper.stationChangeNextById(stationChangeDTO.getId());
        }
        if (chargingStation == null) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        StationNameSearchResponseDTO responseDTO =chargingStationMapper.selectChargersByStationId(chargingStation.getId());
        if (responseDTO == null) {
            return ResponseData.fail(ResponseEnum.FAIL);
        }
        responseDTO.setId(chargingStation.getId());
        responseDTO.setStationName(chargingStation.getStationName());
        responseDTO.setStationLocation(chargingStation.getStationLocation());
        return new ResponseData<>().ok(responseDTO);
    }

    @Override
    public ResponseData<?> initMapStation() {
        List<InitMapStationDTO> list =chargingStationMapper.selectInitMapStation();
        return new ResponseData<>().ok(list);
    }

    @Override
    public ResponseData<?> stationNameSearchWx(String stationName) {
        List<ChargingStation> chargingStationList = chargingStationMapper.stationNameSearch(stationName);
        if (chargingStationList == null) {
            return ResponseData.fail(ResponseEnum.FAIL);
        } else {
            return new ResponseData<>().ok(chargingStationList);
        }

    }

    @Override
    public ResponseData<?> stationNameSearchMap(String stationName) {
        List<InitMapStationDTO> chargingStationList = chargingStationMapper.stationNameSearchMap(stationName);
        return new ResponseData<>().ok(chargingStationList);
    }
}




